/*

	Paper tables
		Makes the tables for the "Reducing Parent-School Information Gaps and Improving Education Outcomes: Evidence from High-Frequency Text Messages" paper

*/

clear
set more off
set excelxlsxlargefile on

*Set directory here at the top
global dir "C:\Users\JULIANMA\Dropbox\Data SMS\submissions\3.jhr\Final MS preparation\Harvard Dataverse"
global path "$dir/"
cd "$path"

run "$dir/scripts/insertrow.ado"
run "$dir/scripts/swaprows.ado"
/*
capture ssc install texsave
capture ssc install outreg2
capture ssc install ivreghdfe
*/


	
**************************************************************************************************************************************************
//////  FIGURE 1: Baseline share of Misinformed Parents 

use "$dir/datasms_final.dta", clear


keep if date==1
label var attendance2013 ""


graph twoway (lowess misinf_gradesall_actas index_risk if constant==1 , lcolor(black) bwidth(0.9)  lpattern(solid) lwidth(medthick)) ///
	(lowess misinf_attendancebooks index_risk if constant==1 , lcolor(black) bwidth(0.9)  lpattern(dash) lwidth(medthick)) ///
	 (histogram index_risk if constant==1 , fcolor(gray%20) lcolor(gray%20)), ///
	xtitle("At-risk index") ytitle("Prop. of parents who misreported child's grades/attendance", size(small)) ///
	 legend(order(1 "Grades misinformation" 2 "Attendance misinformation")) ///
	graphr(color(white)) bgcolor(white) ylabel(, format(%02.1f) nogrid) ///
	plotregion(lcolor(black))

graph export "$path/Auxiliary/results/fig_misinformationbl_index.png", replace


**************************************************************************************************************************************************
//////  TABLE 1: BALANCE

use "$dir/datasms_final.dta", clear

	// baseline 2013
	keep if date==1  
	
	gen missing2013=(math_sr==. | attendance_sr==.)
   
	foreach v in student guardian{
		gen `v'mis=(`v'_completed==0)
	}	
	

	//estimation admin
	local varlist "female age new2014 language_sr math_sr average_sr attendance_sr pass_end_2013 index_risk missing2013"
	local j: word count `varlist'
	tabstat `varlist' if constant==1, by(T) statistics(mean) format(%9.2f) save
	matrix C = r(Stat2)',r(Stat1)'
	tabstat `varlist' if constant==1, statistics(N) format(%9.2f) save
	matrix N = r(StatTotal)'
	matrix P  = J(`j',1,.)

	local i = 1
	foreach var of local varlist {
		areg `var' T if constant==1, a(strata) cluster(strata) 
		if _b[T]!=0 matrix P[`i',1]= 2*(1-normal(abs(_b[T]/_se[T])))
 		qui reg `var' T i.strata if constant==1 
		estimates store `var'
		
		local i= `i'+1
	}
 
    suest `varlist', cluster(strata)
	test [female_mean]T=[age_mean]T=[new2014_mean]T=[language_sr_mean]T= ///
	[math_sr_mean]T=[average_sr_mean]T=[attendance_sr_mean]T=[pass_end_2013_mean]T=[index_risk_mean]T=0
	matrix TA=.,.,.,r(p)
         
	matrix C1 = N,C,P \ TA
	
	
	//estimation surveys
	local varlist "guardian_habits_std guardian_efficiency_std guardian_support_std guardian_supervision_std guardian_parentinvolv_std guardian_positive_std guardian_scindex_std mother_edu guardianmis student_habits_std student_efficiency_std student_support_std student_supervision_std student_parentinvolv_std student_positive_std student_scindex_std studentmis"
	local j: word count `varlist'
	tabstat `varlist' if constant==1, by(T) statistics(mean) format(%9.2f) save
	matrix O = r(Stat2)',r(Stat1)'
	tabstat `varlist' if constant==1, statistics(N) format(%9.2f) save
	matrix M = r(StatTotal)'
	matrix Q = J(`j',1,.)
	matrix V = J(`j',1,.)
	matrix Y = J(`j',1,.)
	
	local i = 1
	foreach var of local varlist {
		areg `var' T if constant==1, a(strata) cluster(strata) 
		if _b[T]!=0 matrix Q[`i',1]= 2*(1-normal(abs(_b[T]/_se[T])))
	
		qui reg `var' T i.strata if constant==1 
		estimates store v`i'
		
		local i= `i'+1
	}
	suest v1 v2 v3 v4 v5 v6, cluster(strata) 
  	test [v1_mean]T=[v2_mean]T=[v3_mean]T=[v4_mean]T= ///
	[v5_mean]T=[v6_mean]T=0
	matrix T2=.,.,.,r(p)

	suest v10 v11 v12 v13 v14 v15, cluster(strata) 
  	test  [v10_mean]T=[v11_mean]T=[v12_mean]T=[v13_mean]T=[v14_mean]T= ///
	[v15_mean]T=0
	matrix T3=.,.,.,r(p)
		
	matrix A1 = M[1..9,1...],O[1..9,1...],Q[1..9,1...]\T2 \J(1,4,.) \ M[10...,1...],O[10...,1...],Q[10...,1...] \T3


// Balance Main Table 
matrix B1 = C1 \J(1,4,.)\ A1
drop _all
svmat B1

forvalues j = 2(1)4{
  gen aux = string(B1`j', "%9.2f")
  replace aux = "" if aux=="."
  drop B1`j'
  ren aux B`j'
}
replace B3="0.00" if B3=="-0.00"
ren B11 B1

gen Sample=""
order Sample 
replace Sample="\hspace{3mm} Female" if _n==1
replace Sample="\hspace{3mm} Age" if _n==2
replace Sample="\hspace{3mm} New student" if _n==3
replace Sample="\hspace{3mm} Language grade" if _n==4
replace Sample="\hspace{3mm} Math grade" if _n==5
replace Sample="\hspace{3mm} Final avg. grade" if _n==6
replace Sample="\hspace{3mm} Attendance rate" if _n==7
replace Sample="\hspace{3mm} Passed grade" if _n==8
replace Sample="\hspace{3mm} At-risk index (standardized)" if _n==9
replace Sample="\hspace{3mm} Missing grades/attendance/pass data" if _n==10
replace Sample="\hspace{3mm} Multiple hypotheses Wald test" if _n==11

insertrow, place(before) row(24)
insertrow, place(before) row(13)
insertrow, place(before) row(1)

replace Sample="\textit{Panel A: Administrative records}" if _n==1
replace Sample="\textit{Panel B: Parents' Survey Data}" if _n==14
replace Sample="\textit{Panel C: Students' Survey Data}" if _n==26

replace Sample="\hspace{6mm} Study habits" if _n==15 | _n==27
replace Sample="\hspace{6mm} Academic efficiency" if _n==16 | _n==28
replace Sample="\hspace{6mm} Family Support" if _n==17 | _n==29
replace Sample="\hspace{6mm} Low Family Supervision" if _n==18| _n==30
replace Sample="\hspace{6mm} Parent School Involvement" if _n==19 | _n==31
replace Sample="\hspace{6mm} Positive reinforcement" if _n==20 | _n==32
replace Sample="\hspace{6mm} Parent scales index" if _n==21
replace Sample="\hspace{6mm} Students' scales index" if _n==33
replace Sample="\hspace{3mm} Mother completed high school" if _n==22
replace Sample="\hspace{3mm} Missing baseline survey" if _n==23 | _n==34

replace Sample="\hspace{3mm} Multiple hypotheses Wald test" if _n==24 | _n==35

insertrow, place(before) row(27)
insertrow, place(before) row(15)
replace Sample="\hspace{3mm} Standardized scales ($\mu_c=0$, $\sigma_C=1$)" if _n==15 | _n==28


label var B1 "Obs."
label var B2 "Treatment Mean ($\mu_T$)"
label var B3 "Control Mean ($\mu_C$)"
label var B4 "p-value of adj. dif."

tostring B1, replace
replace B1="" if B1=="."
insertrow, place(before) row(1) 
local i=1
foreach var of varlist B1-B4{
 replace `var'="[`i']" in 1
 local i=`i'+1
}
insertrow, place(after) row(1)

#delimit ;
global note1 
"Note: Column [1] shows the number of observations with non-missing data, columns [2] and [3] the mean value of each baseline characteristic in the treated and control group, respectively. Column [4] reports the p-value on the treatment coefficient in a regression using each baseline characteristic as the dependent variable. All regressions include classroom fixed effects and robust standard errors are clustered at this level. Observable variables in Panel A correspond to 2013 except for new student variable that refers to 2014." ;
#delimit cr

texsave using "$path/Auxiliary/results/Table_Balance.tex", width(0.85\textwidth) ///
replace size(scriptsize) align(lCCCC) location(ht) varlabels frag ///
title(Students and parents pre-treatment characteristics) marker(tab:balance) ///
footnote("$note1", size(scriptsize)) nofix


**************************************************************************************************************************************************
////// APPENDIX FIGURE 1: BALANCE
 include "$path\scripts\balance_figure_sms.do" //do file with program that saves relevant estimates for balance figure 


 use "$dir/datasms_final.dta", clear

  // baseline 2013
  keep if date==1
	
  gen missing2013=(math_sr==. | attendance_sr==.)
  foreach v in student guardian{
	 gen `v'mis=(`v'_completed==0)
 }
		
	
  balance_fig1 female age new2014 language_sr math_sr average_sr attendance_sr pass_end_2013 index_risk missing2013, cluster(strata) data(admin_fs)
	
  balance_fig1 guardian_habits_std guardian_efficiency_std guardian_support_std guardian_supervision_std guardian_parentinvolv_std guardian_positive_std guardian_scindex_std mother_edu guardianmis student_habits_std student_efficiency_std student_support_std student_supervision_std student_parentinvolv_std student_positive_std student_scindex_std studentmis, cluster(strata) data(surveys_fs)	
	
  use  "$path/Auxiliary/results/balance_admin_fs.dta", clear   	
  append using  "$path/Auxiliary/results/balance_surveys_fs.dta"

  replace var="Female" if _n==1 
  replace var="Age" if _n==2
  replace var="New student" if _n==3
  replace var="Language grade" if _n==4
  replace var="Math grade" if _n==5
  replace var="Final avg. grade" if _n==6
  replace var="Attendance rate" if _n==7
  replace var="Passed grade" if _n==8
  replace var="At-risk index" if _n==9 
  replace var="Missing grades/attendance/pass data" if _n==10

  replace var="Study habits" if _n==11 | _n==20
  replace var="Academic efficiency" if _n==12 | _n==21
  replace var="Family Support" if _n==13 | _n==22
  replace var="Low Family Supervision" if _n==14 | _n==23
  replace var="Parent School Involvement" if _n==15 | _n==24
  replace var="Positive reinforcement" if _n==16 | _n==25
  replace var="Parent scales index" if _n==17 
  replace var="Student scales index" if _n==26 
  replace var="Mother completed high school" if _n==18
  replace var="Missing baseline survey" if _n==19 | _n==27
 
  drop b_T0 b_T1
  forvalues i=1(1)3{	
	  ren N`i' T0`i'
  }
  ren pvalue1 T04
  ren pvalue2 T05
  ren pvalue3 T06
  ren pvalue4 T07
  ren pvalue5 T08
 
  insobs 2, before(1)
  replace var="." in 1
  replace var="Panel A. Administrative Data" in 2
  insobs 2, before(13)
  replace var="." in 13
  replace var="Panel B. Parent Surveys" in 14
  insobs 2, before(24)
  replace var="." in 24
  replace var="Panel C. Student Surveys" in 25

  gen order=_n
  labmask order, values(var) 
  
	//panel a: alternative samples
    twoway (scatter order T04, mcolor(ebblue) msize(small) msymbol(square)) ///
           (scatter order T05, mcolor(dkorange) msize(small) msymbol(smdiamond)) ///
		   (scatter order T06, mcolor(cranberry) msize(small) msymbol(triangle)),  /// 
			legend(order(1 "Full sample" 2 "Parent respondents" ///
			3 "Student respondents") size(vsmall)) ///		   
		   xlabel(0 0.05 0.1 1, notick labs(vsmall) ) ///
		   xtitle("p-value", size(small)) ///
		   ylabel(1(1)33, val notick labs(vsmall) format(%9.2f) /// 
		   labgap(*3) angle(horizontal)) yscale(rev r(1(1)29))  ///
		   ytitle("") ///
           xline(0.05, lp(dot) lc(black) lw(medthin)) ///
		   xline(0.1, lp(dot) lc(black) lw(medthin)) ///
		   graphr(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
	graph export "$path/Auxiliary/results/fig_balance_pa.png", replace	
		   
   //panel b: spillovers specfication
   twoway (scatter order T07, mcolor(teal) msize(small) msymbol(circle)) ///
           (scatter order T08, mcolor(teal) msize(small) msymbol(circle_hollow)),  /// 
		   legend(order(1 "T coeff." 2 "TxHigh-Share coeff.") size(vsmall)) ///		   
		   xlabel(0 0.05 0.1 1, notick labs(vsmall) ) ///
		   xtitle("p-value", size(small)) ///
		   ylabel(1(1)33, val notick labs(vsmall) format(%9.2f) /// 
		   labgap(*3) angle(horizontal)) yscale(rev r(1(1)29))  ///
		   ytitle("") ///
           xline(0.05, lp(dot) lc(black) lw(medthin)) ///
		   xline(0.1, lp(dot) lc(black) lw(medthin)) ///
		   graphr(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
	graph export "$path/Auxiliary/results/fig_balance_pb.png", replace	
	  
  
  erase "$path/Auxiliary/results/balance_admin_fs.dta"
  erase "$path/Auxiliary/results/balance_surveys_fs.dta"
  
	  

**************************************************************************************************************************************************
////// TABLE 2: COMPLIANCE  (AND APPENDIX TABLE 2)

use "$dir/datasms_final.dta", clear


//// Main Compliance Table
	//estimation(SMS sent)
	global outregoptions = "dec(3) bracket excel nocons nor2 noobs nonotes dta"
	global covariates "attendance2013 math_std2013 attendance2013du math_imp"

	local j = 1
	foreach data in all attendance behavior grades gral{ 
		if `j'==1 local rep = "replace"
		if `j'>1  local rep = ""
		
		sum sms_sent_`data' if T==0 & constant==1
		local mean=string(`r(mean)',"%9.3f")

		//main sample
		areg sms_sent_`data' T ${covariates} i.date if constant==1, a(strata) cluster(strata) 
		local N = e(N)
		outreg2 using "$path/Auxiliary/results/T_smssent.xml", `rep' keep(T) $outregoptions addstat("Observations", `N',"Control mean", `mean' ) 

		//full sample
		areg sms_sent_`data' T ${covariates}  i.date, a(strata) cluster(strata) 
		local N = e(N)
		outreg2 using "$path/Auxiliary/results/T_smssent_fs.xml", `rep' keep(T) $outregoptions addstat("Observations", `N') 	
		local j = `j'+1
	}

	//estimation( SMS received)
	local j = 1
	foreach data in all attendance behavior grades gral{ 
		if `j'==1 local rep = "replace"
		if `j'>1  local rep = ""
		
		sum sms_received_`data'  if T==0 & constant==1 
		local mean=string(`r(mean)',"%9.3f")
		
		sum DT_`data' if T==1 & constant==1 
		local mean_prc=string(`r(mean)',"%9.3f")	
		
		
		//main sample
		areg sms_received_`data' T ${covariates}  i.date if constant==1, a(strata) cluster(strata) 
		local N = e(N)		
		outreg2 using "$path/Auxiliary/results/T_smsreceived.xml", `rep' keep(T) $outregoptions addstat("Observations", `N', "Control mean", `mean', "\% text messages received / sent (among treated)", `mean_prc')

		//full sample
		areg sms_received_`data' T ${covariates}  i.date, a(strata) cluster(strata) 
		local N = e(N)  
		outreg2 using "$path/Auxiliary/results/T_smsreceived_fs.xml", `rep' keep(T) $outregoptions addstat("Observations", `N')
		local j = `j'+1
	}


//// Appendix Table: Compliance by year  
//estimation( SMS sent)
global outregoptions = "dec(3) bracket excel nocons nor2 noobs nonotes dta"
foreach c in 2014 2015{
local j = 1	
foreach data in all attendance behavior grades gral{ 

	if `j'==1 local rep = "replace"
	if `j'>1  local rep = ""

	areg sms_sent_`data' T  ${covariates} if year_admin==`c' & constant==1 , a(strata) cluster(strata) 
	local N = e(N)
	sleep 2000
	outreg2 using "$path/Auxiliary/results/T_sent`c'.xml", `rep' keep(T) $outregoptions addstat("Observations", `N') 
	local j = `j'+1
}
local j = 1
//estimation(SMS received)
foreach data in all attendance behavior grades gral{ 
	if `j'==1 local rep = "replace"
	if `j'>1  local rep = ""

	areg sms_received_`data' T  ${covariates} if year_admin==`c' & constant==1, a(strata) cluster(strata) 
	local N = e(N)
	sleep 2000
	outreg2 using "$path/Auxiliary/results/T_received`c'.xml", `rep' keep(T) $outregoptions addstat("Observations", `N') 
	local j = `j'+1
}
}

preserve
collapse (sum)  v2=sms_sent_all v3=sms_sent_attendance v4=sms_sent_behavior v5=sms_sent_grades v6=sms_sent_gral 
forvalues i=3(1)6{ 
	replace v`i'=v`i'/v2

} 
replace v2=.
gen v1=.
order v1
forvalues i=1(1)6{ 
	tostring v`i', replace force  format(%9.3f)
	replace  v`i'="" if v`i'=="."

} 

save "$path/Auxiliary/results/prop_sent.dta", replace
restore

preserve
collapse (sum)  v2=sms_received_all v3=sms_received_attendance v4=sms_received_behavior v5=sms_received_grades v6=sms_received_gral 
forvalues i=3(1)6{ 
	replace v`i'=v`i'/v2

} 
replace v2=.
gen v1=.
order v1
forvalues i=1(1)6{ 
	tostring v`i', replace force  format(%9.3f)
	replace  v`i'="" if v`i'=="."

} 

save "$path/Auxiliary/results/prop_received.dta", replace
restore

//// Appendix Table: Compliance with Surveys
	/*Only 2015 available*/

local j=1
local outcomes "gr_sms_all gr_sms_attendance gr_sms_behavior gr_sms_grades" 
foreach var of local outcomes{
	if `j'==1 local rep = "replace"
	if `j'>1  local rep = ""
	sum `var' if T==0 & constant==1
	local mean=r(mean)
	areg `var' T ${covariates} if constant==1 , a(strata) cluster(strata)  
	local N = e(N)
	outreg2 using "$path/Auxiliary/results/compliance_surveys_gr.xml", `rep' keep(T) $outregoptions addstat("Observations", `N', "Control mean", `mean')
	local j = `j'+1
}

	
//// Compile Main Compliance Table
use "$path/Auxiliary/results/T_smssent_dta.dta",clear
append using "$path/Auxiliary/results/T_smsreceived_dta.dta"
append using "$path/Auxiliary/results/prop_sent.dta"
append using "$path/Auxiliary/results/prop_received.dta"

drop if _n==1 | _n==2 | _n==3 | _n==7 | _n==9  | _n==10 | _n==11 
insertrow, place(after) row(8)
swaprows, swap(4) and(9)

   
foreach var of varlist v3-v5{
	replace `var'="0.000" if _n==9  | _n==10 
	
}
replace v1="Text messages sent" if _n==1 
replace v1="Text messages received" if _n==5
replace v1="Control mean messages sent" if _n==9
replace v1="Control mean messages received" if _n==10
replace v1="Proportion of messages across type (sent)" if _n==12
replace v1="Proportion of messages across type (received)" if _n==13

label var v2 "All"
label var v3 "Attendance"
label var v4 "Behavior"
label var v5 "Grades" 
label var v6 "General"

insertrow, place(before) row(1)
local i=1
foreach var of varlist v2-v6{
	replace `var'="[`i']" in 1
	local i=`i'+1
}
insertrow, place(after) row(1)



#delimit ;
global note1 
"Note: Text messages sent refers to the cumulative number of text messages sent to student's parents. Text messages received refers to the cumulative number of text messages with a confirmed delivery status. Columns [2]-[5] report the Ticjg coefficient of equation (\ref{eq:1}) with the annual number of each type of text message as the dependent variable. Column [1] adds all types of text messages. Attendance, grades, and classroom behavior text messages were sent only to the treatment group. General text messages were sent to all treatment and control individuals. All models include the baseline math grade, attendance rate as control variables, classroom (randomization strata) and year fixed effects. If baseline values of baseline math grade/attendance were missing, we imputed them using the classroom-level mean and added an indicator variable for these imputed observations. Standard errors are clustered at the classroom level (shown in brackets). * significant at 10\%; ** significant at 5\%; *** significant at 1\%." ;
#delimit cr
texsave using "$path/Auxiliary/results/Table_Compliance.tex", width(\textwidth) ///
replace size(scriptsize) align(lCCCCC) location(ht) varlabels  frag hlines(9) ///
title(Compliance by type of text message) marker(tab:compliance) ///
footnote("$note1", size(scriptsize)) nofix


erase "$path/Auxiliary/results/T_smssent.txt"
erase "$path/Auxiliary/results/T_smsreceived.txt"
erase "$path/Auxiliary/results/T_smssent.xml"
erase "$path/Auxiliary/results/T_smsreceived.xml"  
erase "$path/Auxiliary/results/T_smssent_dta.dta"
erase "$path/Auxiliary/results/T_smsreceived_dta.dta"
erase  "$path/Auxiliary/results/prop_sent.dta"
erase  "$path/Auxiliary/results/prop_received.dta" 



//// Compile Appendix Table Compliance (By Year and Full Sample and Surveys) 
use "$path/Auxiliary/results/T_sent2014_dta.dta",clear
append using "$path/Auxiliary/results/T_received2014_dta.dta"
append using "$path/Auxiliary/results/T_sent2015_dta.dta"
append using "$path/Auxiliary/results/T_received2015_dta.dta"

append using "$path/Auxiliary/results/T_smssent_fs_dta.dta"
append using "$path/Auxiliary/results/T_smsreceived_fs_dta.dta"

append using "$path/Auxiliary/results/compliance_surveys_gr_dta.dta"


drop if v1=="Students"
drop if _n==1 | _n==2 | _n==6  | _n==7  | _n==8 | _n==9 | _n==15 | _n==16 | _n==21 | _n==22 | _n==23 | _n==24 | _n==29 | _n==30 | _n==35 | _n==36 | _n==37  | _n==38  | _n==43  | _n==44  | _n==50


replace v1 = "\textit{Panel A: 2014}" in 1
replace v1 = "\textit{Panel B: 2015}" in 9
replace v1 = "\textit{Panel C: Full Sample}" in 17
replace v1 = "\textit{Panel D: Parent Surveys 2015}" in 25

replace v1="\hspace{3mm}  Observations" if v1=="Observations"


replace v1="\hspace{3mm} Text messages sent" if _n==2 | _n==10 | _n==18
replace v1="\hspace{3mm} Text messages received" if _n==5 | _n==13 | _n==21
replace v1="\hspace{3mm} Declares to have received text messages" if _n==26

insertrow, place(before) row(25)
insertrow, place(before) row(17)
insertrow, place(before) row(9)


label var v2 "All"
label var v3 "Attendance"
label var v4 "Behavior"
label var v5 "Grades" 
label var v6 "General"

insertrow, place(before) row(1)
local i=1
foreach var of varlist v2-v6{
	replace `var'="[`i']" in 1
	local i=`i'+1
}
insertrow, place(after) row(1)

replace v6="-" if _n==31 | _n==32 | _n==34


#delimit ;
global note1 
"Note: Text messages sent refers to the cumulative number of text messages sent to student's parents in the full sample. Text messages received refers to the cumulative number of text messages with a confirmed delivery status. Columns [2]-[5] report the Ticjg coefficient of equation (\ref{eq:1}) with the annual number of each type of text message as the dependent variable. Column [1] adds all types of text messages. Attendance, grades, and classroom behavior text messages were sent only to the treatment group. General text messages were sent to all treatment and control individuals. All models include the baseline math grade and attendance rate as control variables and grade dummies. If baseline values are missing, we impute them using the classroom-level mean and flag these observations in the regression. Regressions additionally include year and classroom fixed effects and robust standard errors are clustered at the classroom level. * significant at 10\%; ** significant at 5\%; *** significant at 1\%." ;
#delimit cr
texsave using "$path/Auxiliary/results/Appendix_Table_Compliance.tex", width(\textwidth) ///
replace size(scriptsize) align(lCCCCC) location(ht) varlabels  frag ///
title(Compliance in different samples) marker(tab:compliance_appendix)  hlines(10 19 28) ///
footnote("$note1", size(scriptsize)) nofix

erase "$path/Auxiliary/results/T_smssent_fs.txt"
erase "$path/Auxiliary/results/T_smsreceived_fs.txt"
erase "$path/Auxiliary/results/T_smssent_fs.xml"
erase "$path/Auxiliary/results/T_smsreceived_fs.xml"  
erase "$path/Auxiliary/results/T_smssent_fs_dta.dta"
erase "$path/Auxiliary/results/T_smsreceived_fs_dta.dta"

 foreach c in 2014 2015{
	erase "$path/Auxiliary/results/T_sent`c'.txt"
	erase "$path/Auxiliary/results/T_received`c'.txt"
	erase "$path/Auxiliary/results/T_sent`c'.xml"
	erase "$path/Auxiliary/results/T_received`c'.xml"
	erase "$path/Auxiliary/results/T_sent`c'_dta.dta"	
	erase "$path/Auxiliary/results/T_received`c'_dta.dta"	
}

erase "$path/Auxiliary/results/compliance_surveys_gr.xml"  
erase "$path/Auxiliary/results/compliance_surveys_gr.txt"
erase "$path/Auxiliary/results/compliance_surveys_gr_dta.dta" 	



*************************************************************************************************************************************************
////TABLE 3: MAIN RESULTS
include "$path\scripts\specchartSMS.do" //program for robustness figure

use "$dir/datasms_final.dta", clear

//vars for fs estimation
gen attendance_fs=attendance_cb
gen highattendance_fs=highattendance_cb
gen highgrade_fs=highgrade_cb


//interaction T*at-risk index 
gen Tindex_risk=T*index_risk

//Estimation
global outregoptions = "dec(3) bracket excel nor2 noobs nonotes dta slow(2000)"
global covariates "attendance2013 math_std2013 attendance2013du"
local j = 1
local outcomes "math_std highgrade attendance highattendance beh_neg_std" 
foreach var of local outcomes {
	if `j'==1 local rep = "replace"
	if `j'>1  local rep = ""
		
	// main results
	sum `var'_cb if constant==1 & T==0
	local mean=`r(mean)'
	areg `var'_cb T ${covariates} i.date if constant==1, ab(strata) cluster(strata) 	
	local N = e(N)

	outreg2 using "$path/Auxiliary/results/itt_main.xml",`rep' keep(T) $outregoptions nocons  addstat("Observations", `N', "Control mean", `mean') 

	
	specchart  T,spec(main main_sample data_books controls pooled) replace save("$path/Auxiliary/results/`var'_estimates") 
	
	// HE by at-risk index
	areg `var'_cb T Tindex_risk index_risk ${covariates}  i.date if constant==1, ab(strata) cluster(strata)
	outreg2 using "$path/Auxiliary/results/itt_indexrisk.xml",`rep' keep(T Tindex_risk) $outregoptions nocons  addstat("Observations", `N', "Control mean", `mean')

	//No controls
	areg `var'_cb T i.date if constant==1, ab(strata) cluster(strata)
	local N = e(N)
	outreg2 using "$path/Auxiliary/results/itt_nocontrols.xml",`rep' keep(T) $outregoptions nocons addstat("Observations", `N', "Control mean", `mean')
	specchart  T,spec(main_sample data_books pooled) save("$path/Auxiliary/results/`var'_estimates")
	
	//by cohort
	local h=1
	foreach y in 2014 2015{
		if `h`''==1 local rep = "replace"
		if `h'>1  local rep = ""
		areg `var'_cb T ${covariates} if year_admin==`y' & constant==1, ab(strata) cluster(strata) 
		local N = e(N)
		outreg2 using "$path/Auxiliary/results/itt_`y'.xml",`rep' keep(T) $outregoptions nocons  addstat("Observations", `N')
		specchart  T,spec(main_sample data_books controls year_`y') save("$path/Auxiliary/results/`var'_estimates")	
	
		local h = `h'+1
	}
	
	
	//FS with controls
	sum `var'_fs if T==0 
	local mean=`r(mean)'	
	areg `var'_fs T attendance2013 math_std2013_fs attendance2013du  i.date, ab(strata) cluster(strata)
	local N = e(N)
	outreg2 using "$path/Auxiliary/results/itt_fs.xml",`rep' keep(T) $outregoptions nocons addstat("Observations", `N', "Control mean", `mean')
	specchart  T,spec(full_sample data_books controls pooled) save("$path/Auxiliary/results/`var'_estimates")	
	
	// School-records/ACTAS
	if "`var'"!="beh_neg_std"{
		areg `var'_sr T ${covariates} i.date i.math_imp if constant==1 , ab(strata) cluster(strata)
		local N = e(N)
		outreg2 using "$path/Auxiliary/results/itt_sr.xml",`rep' keep(T) $outregoptions nocons addstat("Observations", `N')
		specchart  T,spec(main_sample data_actas controls pooled) save("$path/Auxiliary/results/`var'_estimates")		
	}

	
	
	local j = `j'+1

}




////FIGURE 3
local outcome "math_std_cb attendance_cb beh_neg_std_cb"
local he "index_risk"
local bl "at risk-index"
global condition "if constant==1" 
cap drop n
bys id: gen n=_n
include "$path/scripts/HE_percentiles_figure.do"


//// Compile Main Results Table
use "$path/Auxiliary/results/itt_main_dta.dta", clear
append using "$path/Auxiliary/results/itt_indexrisk_dta.dta"


replace v1="\textit{Panel A: Treatment Effects}" if _n==3 
replace v1="\textit{Panel B: Heterogeneity}" if _n==11

replace v1="\hspace{3mm} Observations" if v1=="Observations"
replace v1="\hspace{3mm} Control mean" if v1=="Control mean"

replace v1="\hspace{3mm} T x at-risk index" if v1=="Tindex_risk" 
replace v1="\hspace{3mm} T" if v1=="T"


drop if (_n==1 | _n==2 | _n==7| _n==8 | _n==9| _n==10 )


label var v2 "Standard. math grade" 
label var v3 "Math grade $>$4.0" 
label var v4 "Attendance rate" 
label var v5 "Cumulative attendance $>$85\%"  
label var v6 "Standard. $\#$ negative beh. notes" 

insertrow, place(before) row(1)
local i=1
foreach var of varlist v2-v6{
	replace `var'="[`i']" in 1
	local i=`i'+1
}
insertrow, place(after) row(1)

replace v2="0.00" if _n==14
replace v6="0.00" if _n==14


#delimit ;
global note1 
"Note: ITT refers to the intention-to-treat effects estimates shown on each column for each outcome. Coefficients of equation (\ref{eq:1}) were estimated using OLS. ATT refers to the estimates of the average treatment effects on the treated shown on each column for each outcome. Coefficients of equation (\ref{eq:2}) were estimated using IV where the share of topic-specific text messages received over those sent was instrumented with the randomized assignment to treatment. All models include the baseline math grade, attendance rate as control variables, classroom (randomization strata), year and grade-level fixed effects. If baseline values of baseline math grade/attendance were missing, we imputed them using the classroom-level mean and added an indicator variable for these imputed observations. Standard errors are clustered at the classroom level (shown in brackets). * significant at 10\%; ** significant at 5\%; *** significant at 1\%." ;
#delimit cr

texsave using "$path/Auxiliary/results/Table_MainResults.tex", width(\textwidth) ///
replace size(scriptsize) align(lCCCCC) location(ht) varlabels  frag hlines(12) ///
title(Treatment Effects on Grades, Attendance and Behavior) marker(tab:main_results) ///
footnote("$note1", size(scriptsize)) nofix



******************************************************************************
// APPENDIX FIGURE 2: ROBUSTNESS

include "$path\scripts\fig_robustness.do"

// erase
foreach r in  nocontrols 2014 2015 sr{
	erase "$path/Auxiliary/results/itt_`r'.txt"
	cap erase "$path/Auxiliary/results/itt_`r'.xml"
	erase "$path/Auxiliary/results/itt_`r'_dta.dta"


} 

*************************************************************************************************************************************************
////APPENDIX TABLE 3: LATE   

use "$dir/datasms_final.dta", clear


//interactions
foreach d in grades attendance behavior{
	gen d_`d'_index_risk= d_`d'*index_risk
}

gen Tindex_risk=T*index_risk


//Estimation
global outregoptions = "dec(3) bracket excel nor2 noobs nonotes dta slow(2000)"
global covariates "attendance2013 math_std2013 attendance2013du"
local j = 1
local outcomes "math_std highgrade attendance highattendance beh_neg_std" 
foreach var of local outcomes {
	preserve
	if `j'==1 local rep = "replace"
	if `j'>1  local rep = ""
	
	if ("`var'"=="math_std" | "`var'"=="highgrade") rename (d_grades d_grades_index_risk) (d d_index_risk)	
	if ("`var'"=="attendance" | "`var'"=="highattendance") rename (d_attendance d_attendance_index_risk) (d d_index_risk)	
	if ("`var'"=="beh_neg_std") rename (d_behavior d_behavior_index_risk) (d d_index_risk)
		
	//main	
	sum `var'_cb if constant==1 & T==0 
	local mean=`r(mean)'
	ivreghdfe `var'_cb (d=T) ${covariates} i.date if constant==1, ab(strata) cluster(strata) 	
	local N = e(N)
	outreg2 using "$path/Auxiliary/results/ate_main.xml",`rep' keep(d) $outregoptions nocons 

	// HE by at-risk index
	ivreghdfe `var'_cb (d d_index_risk=T Tindex_risk)  index_risk ${covariates}  i.date if constant==1, ab(strata) cluster(strata)
	outreg2 using "$path/Auxiliary/results/ate_indexrisk.xml",`rep' keep(d d_index_risk) $outregoptions nocons  addstat("Observations", `N', "Control mean", `mean')
	
	restore
	local j = `j'+1

}



//// Compile LATE Results Table
use "$path/Auxiliary/results/ate_main_dta.dta", clear
append using "$path/Auxiliary/results/ate_indexrisk_dta.dta"


replace v1="\textit{Panel A: LATE}" if _n==3 
replace v1="\textit{Panel B: Heterogeneity}" if _n==9

replace v1="\hspace{3mm} Observations" if v1=="Observations"
replace v1="\hspace{3mm} Control mean" if v1=="Control mean"

replace v1="\hspace{3mm} D x at-risk index" if v1=="d_index_risk" 
replace v1="\hspace{3mm} D" if v1=="d"
drop if v1=="Students"


drop if (_n==1 | _n==2 | _n==7| _n==8 )


label var v2 "Standardized math grade" 
label var v3 "Math grade $>$4.0" 
label var v4 "Attendance rate" 
label var v5 "Cumulative attendance $>$85\%"  
label var v6 "Standardized $\#$ negative beh. notes" 

insertrow, place(before) row(1)
local i=1
foreach var of varlist v2-v6{
	replace `var'="[`i']" in 1
	local i=`i'+1
}
insertrow, place(after) row(1)

replace v2="0.00" if _n==14
replace v6="0.00" if _n==14


#delimit ;
global note1 
"Note: Panel A shows estimates of the local average treatment effects (LATE) shown on each column for each outcome. Let $D_{icjg}$ be an indicator variable equal to one for those treated students whose parents received at least one text message with information on each specific outcome (i.e., compliers). $D_{itcjg}$ --instead of $T_{icjg}$-- is included in equation (\ref{eq:1}) which we instrument in a first stage with the randomized treatment variable $T_{icjg}$. Panel B adds the interaction with the student-level at risk index. At-risk index is a simple average of standardized baseline attendance, math grades and negative behavioral notes. All models include the baseline math grade, attendance rate as control variables, classroom (randomization strata) and year fixed effects. If baseline values of baseline math grade/attendance were missing, we imputed them using the classroom-level mean and added an indicator variable for these imputed observations. Models in Panel B additionally include the at-risk index variable as control. Columns 1 and 5 report results on outcomes that were standardized so that mean among the control students is zero and the standard deviation is one.  Standard errors are clustered at the classroom level (shown in brackets). * significant at 10\%; ** significant at 5\%; *** significant at 1\%." ;
#delimit cr

texsave using "$path/Auxiliary/results/Table_ATEResults.tex", width(\textwidth) ///
replace size(scriptsize) align(lCCCCC) location(ht) varlabels  frag hlines(12) ///
title(Local Average Treatment Effects) marker(tab:ate_results) ///
footnote("$note1", size(scriptsize)) nofix

foreach r in main indexrisk{
	erase "$path/Auxiliary/results/ate_`r'.xml"
	erase "$path/Auxiliary/results/ate_`r'.txt"
	erase "$path/Auxiliary/results/ate_`r'_dta.dta"

}


 *************************************************************************************************************************************************

////TABLE 4: SPILLOVERS ON THE TREATED
use "$dir/datasms_final.dta", clear

//interaction T*at-risk index 
gen Tindex_risk=T*index_risk

//Estimation
global outregoptions = "dec(3) bracket excel nor2 noobs nonotes dta slow(2000)"
global covariates "attendance2013 math_std2013 attendance2013du"
local j = 1
local outcomes "math_std highgrade attendance highattendance beh_neg_std" 
foreach var of local outcomes {
	if `j`''==1 local rep = "replace"
	if `j'>1  local rep = ""
		
	sum `var'_cb if constant==1 & T==0 
	local mean=`r(mean)'
	areg `var'_cb T TR ${covariates}  i.date if constant==1, ab(strata) cluster(strata) 	
	local N = e(N)
	test T+TR=0
	local p = string(`r(p)',"%9.2f")		
	outreg2 using "$path/Auxiliary/results/itt_spillovers.xml",`rep' keep(T TR) $outregoptions nocons  addstat("Observations", `N',"Control mean", `mean')  addtext("p-value H_{0}: T+TxH=0", `p')
	
	local j = `j'+1

}

//compile table
use "$path/Auxiliary/results/itt_spillovers_dta.dta", clear

replace v1="T x High-Share" if v1=="TR" 

drop if (_n==1 | _n==2  )

label var v2 "Standard. math grade" 
label var v3 "Math grade $>$4.0" 
label var v4 "Attendance rate" 
label var v5 "Cumulative attendance $>$85\%"  
label var v6 "Standard. $\#$ negative beh. notes" 

local i=1
foreach var of varlist v2-v6{
	replace `var'="[`i']" in 1
	local i=`i'+1
}
swaprows, swap(8) and(9)

replace v2="0.00" in 8
replace v6="0.00" in 8
insertrow, place(after) row(1)


#delimit ;
global note1 
"Note: Panel A shows intention-to-treat effects estimates from equation (X) shown on each column for each outcome. Coefficients were estimated using OLS. T refers to the randomized individual-level treatment (equal to 1 if parents were sent text messages and zero otherwise). High-Share refers to the randomized classroom-level treatment (equal to 1 for high-share classrooms and zero for low-share classrooms). Panel B adds the interactions with the student-level at risk index. At-risk index is a simple average of standardized baseline attendance, math grades and negative behavioral notes. All models include the baseline math grade, attendance rate as control variables, classroom (randomization strata), year and grade-level fixed effects. If baseline values of baseline math grade/attendance were missing, we imputed them using the classroom-level mean and added an indicator variable for these imputed observations. Models in Panel B additionally include the at-risk index variable as control. Standard errors are clustered at the classroom level (shown in parentheses). * significant at 10\%; ** significant at 5\%; *** significant at 1\%." ;
#delimit cr

texsave using "$path/Auxiliary/results/Table_SpilloverResults_ITT.tex", width(\textwidth) ///
replace size(scriptsize) align(lCCCCC) location(ht) varlabels frag hlines(7) ///
title(Spillover Effects (ITT)) marker(tab:spillovers_itt) ///
footnote("$note1", size(scriptsize)) nofix


erase "$path/Auxiliary/results/itt_spillovers_dta.dta"
erase "$path/Auxiliary/results/itt_spillovers.xml"
erase "$path/Auxiliary/results/itt_spillovers.txt"


*****************************************************************************************************
// FIGURE 4: TREATMENT EFFECTS IN DAILY ATTENDANCE

use  "$dir/attendance_daily.dta", clear


// interactions T*dow
ta dow, gen(dow_)
foreach var of varlist dow_*{
	gen T`var'=T*`var'
}

label var Tdow_1 "Monday"
label var Tdow_2 "Tuesday"
label var Tdow_3 "Wednesday"
label var Tdow_4 "Thursday"
label var Tdow_5 "Friday"


// daily estimates
global outregoptions = "dec(3) bracket excel nor2 noobs nonotes dta slow(2000)"
global covariates "attendance2013 math_std2013 attendance2013du"

reghdfe attendance_daily Tdow_1 Tdow_2 Tdow_3 Tdow_4 Tdow_5  ${covariates} i.dow if constant==1, ab(strata mdate) cluster(strata) 
local N = e(N)
test Tdow_1=Tdow_2=Tdow_3=Tdow_4=Tdow_5
local p1 = string(`r(p)',"%9.3f")	
test Tdow_1=Tdow_5
local p2 = string(normal(sign(_b[Tdow_5]-_b[Tdow_1])*sqrt(r(F))),"%9.3f")

outreg2 using "$path/Auxiliary/results/daily_attendance.xml",replace keep(Tdow_1 Tdow_2 Tdow_3 Tdow_4 Tdow_5) $outregoptions nocons addstat(N, `N') addtext("p-value of equal coeff.",`p1', "p-value of TxMonday $=$ TxFriday$^{\dagger}$", `p2')


 //figure
	qui coefplot, keep(Tdow_1 Tdow_2 Tdow_3 Tdow_4 Tdow_5) vertical  yline(0, lwidth(medium) lcolor(black%80)) ///
	ciopts(recast(rcap) lcolor(black)) mfcolor(black) mlcolor(black) level(90)  graphr(color(white)) bgcolor(white) ylabel(, nogrid)
	graph export "$path/Auxiliary/results/fig_attendance_daily.png", as(png) replace


 //Compile daily results
 use "$path/Auxiliary/results/daily_attendance_dta.dta", clear
 
 swaprows, swap(17) and(14)
 insertrow, place(before) row(14)
 drop if _n==18
 
 drop if (_n==1 | _n==2 | _n==3 )
 
 replace v1="T x Monday" if _n==1
 replace v1="T x Tuesday" if _n==3 
 replace v1="T x Wednesday" if _n==5
 replace v1="T x Thursday" if _n==7
 replace v1="T x Friday" if _n==9
 replace v1="Observations" if v1=="N"
 
 label var v2 "Daily Attendance" 

 insertrow, place(before) row(1)

#delimit ;
 global note1 
 
"Note: Table shows intention-to-treat estimates (T) by day of the week estimated using OLS. Attendance outcome is measured at a daily basis. T refers to the randomized treatment (equal to 1 if parents were sent text-messages and zero otherwise) and is interacted with each day-of-the-week indicator variables. All models include the day-of-the-week indicator variables as controls, baseline math grade, attendance rate as control variables, classroom (randomization strata), and month x year fixed effects. If baseline values of baseline math grade/attendance were missing, we imputed them using the classroom-level mean and added an indicator variable for these imputed observations. Standard errors are clustered at the classroom level (shown in brackets). * significant at 10\%; ** significant at 5\%; *** significant at 1\%.";
#delimit cr


 texsave using "$path/Auxiliary/results/Appendix_Table_DailyAttendance.tex", width(0.5\textwidth) ///
 replace size(scriptsize) align(lC) location(ht)  varlabels frag hlines(12)  ///
 title(Treatment Effects Over the Week (Weekly Fade Out)) marker(tab:daily_attendance) ///
 footnote("$note1", size(scriptsize)) nofix
 

erase "$path/Auxiliary/results/daily_attendance_dta.dta"
erase "$path/Auxiliary/results/daily_attendance.xml"
erase "$path/Auxiliary/results/daily_attendance.txt"




*************************************************************************************************************************************************
//// FIGURE 5: TREATMENT EFFECTS OVER TIME

use "$dir/datasms_monthly.dta", clear


local j = 1
foreach d in grades attendance behavior {
	// interaction with relative times
	  //relative time varies depending on the sms type	

	preserve
	gen dtime=.
	replace dtime=1 if time_`d'>=1 & time_`d'<=3
	replace dtime=2 if time_`d'>=4 & time_`d'<=6
	replace dtime=3 if time_`d'>=7 & time_`d'<=9
	replace dtime=4 if time_`d'>=10 & time_`d'<=12
	replace dtime=5 if time_`d'>=13 & time_`d'<=17
	ta dtime, gen(dtime_)


	foreach var of varlist dtime_* {
		gen T`var'=T*`var'

	}

	label var Tdtime_1 "Months 1-3"
	label var Tdtime_2 "Months 4-6"
	label var Tdtime_3 "Months 7-9"
	label var Tdtime_4 "Months 10-12"
	label var Tdtime_5 "Months 13-17"


	//Estimation
	global outregoptions = "dec(3) bracket excel nor2 nonotes dta slow(2000)"
	global covariates "attendance2013 math_std2013 attendance2013du"
	if "`d'"=="grades" local var = "math_month_std"
	if "`d'"=="attendance" local var ="attendance_month"
	if "`d'"=="behavior" local var = "beh_month_neg_std"
 

	if `j'==1 local rep = "replace"
	if `j'>1  local rep = ""
	 	 
	areg `var' Tdtime_1 Tdtime_2 Tdtime_3 Tdtime_4 Tdtime_5 i.dtime  ${covariates} if constant==1, ab(strata) cluster(strata)
	test Tdtime_1=Tdtime_2=Tdtime_3=Tdtime_4=Tdtime_5
	local p1 = r(p)
	local p1 = string(`p1',"%9.3f")		
	test Tdtime_1=Tdtime_5
	local p2 = r(p)
	local p2 = string(`p2',"%9.3f")		
	outreg2 using "$path/Auxiliary/results/dtime.xml",`rep' keep(Tdtime_1 Tdtime_2 Tdtime_3 Tdtime_4 Tdtime_5) $outregoptions nocons addstat("p-value of equal coeff.", `p1')

	/*coefficients graph*/
	qui coefplot, keep(Tdtime_1 Tdtime_2 Tdtime_3 Tdtime_4 Tdtime_5) vertical  yline(0, lwidth(medium) lcolor(black%80))  graphr(color(white)) bgcolor(white) ylabel(,nogrid)  ///
	ciopts(recast(rcap) lcolor(black)) mfcolor(black) mlcolor(black) level(90) 
	graph export "$path/Auxiliary/results/`var'_time_ITT.png", as(png) replace	

	local j = `j'+1	
	restore

}	


//compile
use "$path/Auxiliary/results/dtime_dta.dta", clear

replace v1="T x months 1-3" if _n==4 
replace v1="T x months 4-6" if _n==6
replace v1="T x months 7-9" if _n==8
replace v1="T x months 10-12" if _n==10
replace v1="T x months 13-17" if _n==12


drop if (_n==1 | _n==2  | _n==3 ) 

label var v2 "Standard. math grade" 
label var v3 "Attendance rate " 
label var v4 "Standard. $\#$ negative beh. notes" 

insertrow, place(before) row(1)
local i=1
foreach var of varlist v2-v4{
replace `var'="[`i']" in 1
local i=`i'+1
}
insertrow, place(after) row(1)

#delimit ;
global note1 
" Note: Table reports intention-to-treat (T) estimates for each group-of-months estimated using OLS. Outcomes are measured at a monthly basis. T refers to the randomized treatment (equal to 1 if parents were sent text-messages and zero otherwise) and is interacted with each group-of-months indicator variables. All models include the group-of-months indicator variables as controls, baseline math grade, attendance rate as control variables, classroom (randomization strata), month and year fixed effects. If baseline values of baseline math grade/attendance were missing, we imputed them using the classroom-level mean and added an indicator variable for these imputed observations. Standard errors are clustered at the classroom level (shown in brackets). * significant at 10\%; ** significant at 5\%; *** significant at 1\%.";
#delimit cr

texsave using "$path/Auxiliary/results/Appendix_Table_MonthlyDummies.tex", width(0.8\textwidth) ///
replace size(scriptsize) align(lCCC) location(p) varlabels frag hlines(13) ///
title(Treatment Effects Over Time) marker(tab:het_time) ///
footnote("$note1", size(scriptsize)) nofix

cap erase "$path/Auxiliary/results/dtime.txt"
cap erase "$path/Auxiliary/results/dtime_dta.dta"
cap erase "$path/Auxiliary/results/dtime.xml"



*************************************************************************************************************
//// TABLE 5: INFORMATION GAPS/MISINFORMATION

use  "$dir/datasms_final.dta", clear


//interaction with at-risk index
foreach var of varlist index_risk {
	gen T`var'=T*`var'

}

// Estimation 
global covariates "attendance2013 math_std2013 attendance2013du" 
global outregoptions = "dec(3) bracket excel nor2 noobs nonotes dta slow(2000)"

local j=1
local outcomes "misinf_attendanceall_svs misinf_attendancebooks misinf_gradesall_svs misinf_gradesall_actas misinf_behavior_svs misinf_behavior_books"
foreach var of local outcomes {
  if `j'==1 local rep = "replace"
  if `j'>1  local rep = "" 

  sum `var' if constant==1 & T==0 & date>1 
  local mean= `r(mean)'
  
  areg `var' T  ${covariates} i.date if constant==1 & date>1, ab(strata) cluster(strata)
  local N = e(N)
  outreg2 using "$path/Auxiliary/results/itt_misinf.xml",`rep' keep(T) $outregoptions nocons

  areg `var' T Tindex_risk index_risk ${covariates}  i.date if constant==1 & date>1, ab(strata) cluster(strata)
  local N = e(N)  
  outreg2 using "$path/Auxiliary/results/itt_misinf_he.xml",`rep' keep(T Tindex_risk) $outregoptions nocons addstat("Observations", `N',"Control mean", `mean') 
  
  
  local j=`j'+1
  
}


// misinformation in other subjects (Panel B, Table 6)
local j=1
local outcomes "misinf_gradeslanguage_actas misinf_gradeshistory_actas  misinf_gradesnatural_actas" 
foreach var of local outcomes {
  if `j'==1 local rep = "replace"
  if `j'>1  local rep = "" 

  sum `var' if constant==1 & T==0 & date>1 
  local mean= `r(mean)'
  
  areg `var' T ${covariates}  i.date if constant==1 & date>1 , ab(strata) cluster(strata)
  local N = e(N)        
  outreg2 using "$path/Auxiliary/results/itt_misinf_subj.xml",`rep' keep(T) $outregoptions nocons addstat("Observations", `N', "Control mean", `mean')  
 
  
  local j=`j'+1
  
}


//// Compile Table
use "$path/Auxiliary/results/itt_misinf_dta.dta", clear
append using "$path/Auxiliary/results/itt_misinf_he_dta.dta"

replace v1="\textit{Panel A: Treatment Effects}" if _n==3 
replace v1="\textit{Panel B: Heterogeneity}" if _n==9

replace v1="\hspace{3mm} Observations$^{\dagger}$" if v1=="N" | v1=="Observations"
replace v1="\hspace{3mm} Control mean" if v1=="cmean" | v1=="Control mean"


replace v1="\hspace{3mm} T x at-risk index" if v1=="Tindex_risk" 
replace v1="\hspace{3mm} T" if v1=="T"


drop if (_n==1 | _n==2 | _n==7| _n==8 )

label var v2 "All absenteeism (Surveys)"
label var v3 "All absenteeism (Admin.)"
label var v4 "All grades (Surveys)" 
label var v5 "All grades (Admin.)" 
label var v6 "Misbehavior (Surveys)" 
label var v7 "Misbehavior (Admin.)" 

insertrow, place(before) row(1)
local i=1
foreach var of varlist v2-v7{
replace `var'="[`i']" in 1
local i=`i'+1
}
insertrow, place(after) row(1)

#delimit ;
global note1 
"Note: Panel A shows intention-to-treat effects estimates from equation (\ref{eq:1}) shown on each column for each parental misinformation outcome. Coefficients were estimated using OLS. $T$ refers to the randomized individual-level treatment (equal to 1 if parents were sent text messages and zero otherwise). Panel B shows estimates of the heterogeneity treatment effects by adding the interaction with the baseline at-risk index. At-risk index is a simple average of standardized baseline attendance, math grades and negative behavioral notes. All models include the baseline math grade, attendance rate as control variables, classroom (randomization strata), year and grade-level fixed effects. If baseline values of baseline math grade/attendance were missing, we imputed them using the classroom-level mean and added an indicator variable for these imputed observations. Models in Panel B additionally include the at-risk index variable as control. Column outcomes are indicator variables constructed by contrasting responses in parent surveys with those of student surveys or administrative records (shown in parentheses). Column [1] measures parental misinformation in all absenteeism (with and without parent permission in the previous two weeks) contrasting the responses of parents with those from students. Parents are classified as misinformed if they do not answer at least one of the questions, or if at least one of the answers (in bracket days) provided by students and parents do not match. Column [2] measures misinformation in all absenteeism (with and without permission) contrasting parent responses with classroom books. The ends of original bracket days in absences with and without permission are added to construct new bracket days. Parents are classified as misinformed if they do not answer at least one of the questions, or if classroom books' records of absences over the previous two weeks do not fall in the range. Column [3] contrasts parent and student responses and parents are classified as misinformed if they do not answer, or if answered grades' brackets do not match. Column [4] measures parental misinformation regarding all grades by contrasting parent responses about the student's last end-of-year grades with school records. Parents are treated as misinformed if they do not answer, or if the absolute difference between answered and actual grades is greater than $0.5$. Columns [5] and [6] measure misinformation about student misbehavior by contrasting parent answers with student answers, and with information from classroom books, respectively. Using a four-value scale, parents and students were asked about the degree of agreement with the student's misbehavior statements. For column [5], parents are classified as misinformed if they do not answer at least one of the questions, or if the average absolute difference between parent and student answers are larger than the median ($0.8$). For column [6] parents are treated as misinformed if they do not answer; if the parent's average answer is equal to or larger than the median ($2$), and student did not misbehave according to classroom books; or if the parent's average answer is less than the median answer and student misbehaved in class according to books. Standard errors are clustered at the classroom level (shown in brackets).$^{\dagger}$ Number of observations vary by column because of survey and item non-response. * significant at 10\%; ** significant at 5\%; *** significant at 1\%.";
#delimit cr

texsave using "$path/Auxiliary/results/Table_Misinformation.tex", width(1.2\textwidth) ///
replace size(scriptsize) align(lCCCCCC) location(ht) varlabels frag hlines(12) ///
headerlines("& \multicolumn{2}{c}{Attendance Misinformation} & \multicolumn{2}{c}{Grades Misinformation} & \multicolumn{2}{c}{Behavior Misinformation}" "\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7}")  ///
title(Treatment Effects on Parental Misinformation) marker(tab:misinformation) ///
footnote("$note1", size(tiny)) nofix 


*******************************************************************************
/// TABLE 6: TE ON GRADES AND MIISINFORMATION OTHER SUBJECTS

use "$dir/datasms_final.dta", clear


//Estimation
global outregoptions = "dec(3) bracket excel nor2 noobs nonotes dta slow(2000)"
global covariates "attendance2013 math_std2013 attendance2013du"

local j = 1
local outcomes "language_std history_std naturalscience_std" 
foreach var of local outcomes {
	if `j`''==1 local rep = "replace"
	if `j'>1  local rep = ""
		
	sum `var' if constant==1 & T==0  
	local mean=`r(mean)'
	areg `var' T ${covariates} i.date i.math_imp if constant==1 & date>1 , ab(strata) cluster(strata) 	
	local N = e(N)
	outreg2 using "$path/Auxiliary/results/itt_subj.xml",`rep' keep(T) $outregoptions nocons  addstat("Observations", `N', "Control mean", `mean')

	local j=`j'+1

}


//compile results
use "$path/Auxiliary/results/itt_subj_dta.dta", clear
append using "$path/Auxiliary/results/itt_misinf_subj_dta.dta" //append misinformation panel


replace v1="\hspace{3mm} Observations" if v1=="Observations"
replace v1="\hspace{3mm} Control mean" if v1=="Control mean"
replace v1="\textit{Panel A: Standard. grades}" if _n==3 
replace v1="\textit{Panel B: Misinformation}" if _n==11
replace v1="\hspace{3mm} T" if v1=="T"


drop if (_n==1 | _n==2 | _n==9 | _n==10 )

label var v2 "Language"  
label var v3 "Natural science"  
label var v4 "History"  

insertrow, place(before) row(7)
insertrow, place(before) row(1)
local i=1
foreach var of varlist v2-v4{
	replace `var'="[`i']" in 1
	local i=`i'+1
}
insertrow, place(after) row(1)

foreach var in v2 v3 v4 {
	replace `var'="0.00" in 8
	
}
#delimit ;
global note1 
"Note: Table shows treatment effects estimates on outcomes coming from school administrative records. ITT refers to the intention-to-treat effects estimates shown on each column for each outcome. Coefficients of equation (\ref{eq:1}) were estimated using OLS. ATT refers to the estimates of the average treatment effects on the treated shown on each column for each outcome. Coefficients of equation (\ref{eq:2}) were estimated using IV where the share of topic-specific text messages received over those sent was instrumented with the randomized assignment to treatment. All models include the baseline math grade, attendance rate as control variables, classroom (randomization strata), and year fixed effects. If baseline values of baseline math grade/attendance were missing, we imputed them using the classroom-level mean and added an indicator variable for these imputed observations. Standard errors are clustered at the classroom level (shown in brackets). * significant at 10\%; ** significant at 5\%; *** significant at 1\%." ;
#delimit cr

texsave using "$path/Auxiliary/results/Table_AltOutcomes.tex", width(0.8\textwidth) ///
replace size(scriptsize) align(lCCC) location(ht) varlabels  frag hlines(9) ///
title(Treatment Effects on Other Subjects' Grades and Misinformation) marker(tab:alt_outcomes) ///
footnote("$note1", size(scriptsize)) nofix

// erase
foreach test in itt itt_misinf  {
	erase "$path/Auxiliary/results/`test'_subj.txt"
	cap erase "$path/Auxiliary/results/`test'_subj.xml"
	erase "$path/Auxiliary/results/`test'_subj_dta.dta"

}


*************************************************************************************************************

//// TABLE 7: TREATMENT EFFECTS ON PARENTAL BEHAVIOR AT HOME (SCALES)
use "$dir/datasms_final.dta", clear


// estimates 
global covariates "attendance2013 math_std2013 attendance2013du" 
global outregoptions = "dec(3) bracket excel nor2 noobs nonotes dta slow(2000)"

local j=1
local outcomes "guardian_habits_std guardian_efficiency_std guardian_support_std guardian_supervision_std guardian_parentinvolv_std guardian_positive_std" 
foreach var of local outcomes{
  if `j'==1 local rep = "replace"
  if `j'>1  local rep = "" 

  areg `var' T `var'_bl `var'_bl_du  ${covariates} i.date if date>1 & constant==1, ab(strata) cluster(strata)
  local N = e(N)
  outreg2 using "$path/Auxiliary/results/itt_scales_guardian_bl.xml",`rep' keep(T) $outregoptions nocons addstat(N, `N')   
  
 
  local j=`j'+1
  
}
local j=1
local outcomes "student_habits_std student_efficiency_std student_support_std student_supervision_std student_parentinvolv_std student_positive_std" 
foreach var of local outcomes{
  if `j'==1 local rep = "replace"
  if `j'>1  local rep = "" 
  sum `var' if T==0 & constant==1 
  local mean=r(mean)  
 
  areg `var' T `var'_bl `var'_bl_du  ${covariates}  i.date if date>1 & constant==1, ab(strata) cluster(strata)
  local N = e(N)   
  outreg2 using "$path/Auxiliary/results/itt_scales_student_bl.xml",`rep' keep(T) $outregoptions nocons addstat(N, `N') 
  

  local j=`j'+1
  
}
 
// Compile scales results 
use "$path/Auxiliary/results/itt_scales_guardian_bl_dta.dta", clear
append using "$path/Auxiliary/results/itt_scales_student_bl_dta.dta"

replace v1="\hspace{3mm} Observations$^{\dagger}$" if v1=="N"

replace v1="\textit{Panel A: Parent scales}" if _n==3 
replace v1="\textit{Panel B: Student scales}" if _n==10
replace v1="\hspace{3mm} T" if v1=="T"

drop if (_n==1 | _n==2  | _n==8 | _n==9)


label var v2 "Study habits" 
label var v3 "Academic efficiency"
label var v4 "Family Support" 
label var v5 "Low Family Supervision" 
label var v6 "Parent School Involvement" 
label var v7 "Positive reinforcement" 

insertrow, place(before) row(6)


insertrow, place(before) row(1)
local i=1
foreach var of varlist v2-v7{
replace `var'="[`i']" in 1
local i=`i'+1
}
insertrow, place(after) row(1)

#delimit ;
global note1 
"Note: Table shows intention-to-treat effects estimates from equation (\ref{eq:1}) shown on each column for each outcome. Coefficients were estimated using OLS.  refers to the randomized individual-level treatment (equal to 1 if parents were sent text-messages and zero otherwise). Outcomes are scales built with answers to surveys (see Tables \ref{tab:scales_students_bl} and \ref{tab:scales_parents_bl} for details). Panel A shows results for scales built with answers parents gave to survey questions. Panel B shows results for scales built with answers students gave to survey questions. All models include the baseline math grade, attendance rate and outcome scales, classroom (randomization strata), year and grade-level fixed effects. If baseline values of baseline math grade/attendance or baseline outcomes were missing, we imputed them using the classroom-level mean and added an indicator variable for these imputed observations. Standard errors are clustered at the classroom level (shown in brackets).$^{\dagger}$ Number of observations vary by column because of survey and item non-response. * significant at 10\%; ** significant at 5\%; *** significant at 1\%." ;

#delimit cr

  
texsave using "$path/Auxiliary/results/Table_Scales.tex", width(1.2\textwidth) ///
replace size(scriptsize) align(lCCCCCC) location(ht) varlabels frag hlines(7) ///
title(Treatment Effects on Parental Behavior at Home) marker(tab:scales) ///
footnote("$note1", size(scriptsize)) nofix


foreach s in student guardian{
foreach test in itt  {
	erase "$path/Auxiliary/results/`test'_scales_`s'_bl.txt"
	erase "$path/Auxiliary/results/`test'_scales_`s'_bl.xml"
	erase "$path/Auxiliary/results/`test'_scales_`s'_bl_dta.dta" 
	
}
}


************************************************************************************************************* 
//// TABLE 8: WILLINGNESS TO PAY (WTP) 

use  "$dir/datasms_final.dta", clear

// Indicator variables for randomized prices
forvalues i=1(1)3{
	gen dtipo`i'=(gr_tipo==`i') if gr_tipo!=.
	gen Tdtipo`i'=T*dtipo`i'
	
}



// Estimation
global covariates "attendance2013 math_std2013 attendance2013du" 
global outregoptions = "dec(3) bracket excel nor2 nonotes dta slow(2000)"

// Demand Curve 
areg wtp dtipo2 dtipo3 ${covariates} i.date if date>1 & constant==1, ab(strata) cluster(strata)  
outreg2 using "$path/Auxiliary/results/wtp.xml",replace keep(dtipo2 dtipo3) $outregoptions cons 

// WTP estimates 
areg wtp dtipo2 dtipo3 Tdtipo1 Tdtipo2 Tdtipo3 $covariates i.date if date>1 & constant==1, ab(strata) cluster(strata)
outreg2 using "$path/Auxiliary/results/wtp.xml",append keep(dtipo2 dtipo3 Tdtipo1 Tdtipo2 Tdtipo3) $outregoptions cons  


// Compile WTP results
use "$path/Auxiliary/results/wtp_dta.dta", clear

replace v1="Medium Price" if v1=="dtipo2"
replace v1="High Price" if v1=="dtipo3"

replace v1="T x Low Price" if v1=="Tdtipo1"
replace v1="T x Medium Price" if v1=="Tdtipo2"
replace v1="T x High Price"  if v1=="Tdtipo3"

drop if v1=="N_ind"
drop if (_n==1 | _n==2 )

label var v2 "[1]"
label var v3 "[2]"



#delimit ;
global note1 
"Note: Outcome is an indicator variable for whether the parent reports being willing to pay for continued text message service (4 text messages per month from the school) after the end of the year. Columns [1] and [2] report estimates of being assigned a particular randomized priced (1,500 CLP, 1,000 CLP or 500 CLP, the omitted category) for the complete sample (i.e., both treatment and control groups) and the control-group sample, respectively. Column [3] shows intention-to-treat effects estimates by interacting these randomized prices with the randomized treatment (equal to 1 if parents were sent text messages and zero otherwise). Column [4] shows the same intention-to-treat effects estimates but restricting the sample to those students with high at-risk index (mean). At-risk index is a simple average of standardized baseline attendance, math grades and negative behavioral notes. Coefficients were estimated using OLS. All models include the baseline math grade, attendance rate as control variables, classroom (randomization strata), year and grade-level fixed effects. If baseline values of baseline math grade/attendance were missing, we imputed them using the classroom-level mean and added an indicator variable for these imputed observations. Standard errors are clustered at the classroom level (shown in brackets). * significant at 10\%; ** significant at 5\%; *** significant at 1\%." ;
#delimit cr

texsave using "$path/Auxiliary/results/Table_WTP.tex", width(0.8\textwidth) ///
replace size(scriptsize) align(lCC) location(ht) varlabels frag  hlines(14) ///
title(Treatment Effects on Parental Willingness to Pay (WTP)) marker(tab:wtp) ///
footnote("$note1", size(scriptsize)) nofix


erase "$path/Auxiliary/results/wtp_dta.dta"
erase "$path/Auxiliary/results/wtp.xml"
erase "$path/Auxiliary/results/wtp.txt"

*************************************************************************************************************
//// APPENDIX TABLE 6: TREATMENT EFFECTS ON PARENTAL BEHAVIOR AT HOME (DICOTOMIZED MORE IMP. SCALES VARIABLES)
use "$dir/datasms_final.dta", clear


// estimates 
global covariates "attendance2013 math_std2013 attendance2013du" 
global outregoptions = "dec(3) bracket excel nor2 noobs nonotes dta slow(2000)"

local j=1
local outcomes "dguardian_habits dguardian_efficiency dguardian_support dguardian_supervision dguardian_parentinvolv dguardian_positive" 
foreach var of local outcomes{
  if `j'==1 local rep = "replace"
  if `j'>1  local rep = ""
  sum `var' if T==0 & constant==1 & date>1
  local mean=r(mean)  
  
  areg `var' T `var'_bl `var'_bl_du  ${covariates} i.date if date>1 & constant==1, ab(strata) cluster(strata)
  local N = e(N)
  outreg2 using "$path/Auxiliary/results/itt_dummyscales_guardian_bl.xml",`rep' keep(T) $outregoptions nocons addstat("Control mean", `mean',"N", `N')   
 
  local j=`j'+1
  
}
local j=1
local outcomes "dstudent_habits dstudent_efficiency dstudent_support dstudent_supervision dstudent_parentinvolv dstudent_positive" 
foreach var of local outcomes{
  if `j'==1 local rep = "replace"
  if `j'>1  local rep = "" 
  sum `var' if T==0 & constant==1 & date>1
  local mean=r(mean)  

  areg `var' T `var'_bl `var'_bl_du  ${covariates}  i.date if date>1 & constant==1, ab(strata) cluster(strata)
  local N = e(N)   
  outreg2 using "$path/Auxiliary/results/itt_dummyscales_student_bl.xml",`rep' keep(T) $outregoptions nocons addstat(Control mean, `mean', N, `N') 
  

  local j=`j'+1
  
}
 
// Compile scales results 
use "$path/Auxiliary/results/itt_dummyscales_guardian_bl_dta.dta", clear
append using "$path/Auxiliary/results/itt_dummyscales_student_bl_dta.dta"


replace v1="\hspace{3mm} Observations$^{\dagger}$" if v1=="N"
replace v1="\hspace{3mm} Control mean" if v1=="Control mean"

replace v1="\textit{Panel A: Parent scales}" if _n==3 
replace v1="\textit{Panel B: Student scales}" if _n==11
replace v1="\hspace{3mm} T" if v1=="T"

drop if (_n==1 | _n==2  | _n==9 | _n==10)


label var v2 "Organized for school work" 
label var v3 "Understand difficult content"
label var v4 "Parents show pride" 
label var v5 "Went to school alone" 
label var v6 "Parents contacted teacher" 
label var v7 "Parents congratulated student" 

insertrow, place(before) row(7)


insertrow, place(before) row(1)
local i=1
foreach var of varlist v2-v7{
replace `var'="[`i']" in 1
local i=`i'+1
}
insertrow, place(after) row(1)

#delimit ;
global note1 
"Note: Table shows intention-to-treat effects estimates from equation (\ref{eq:1}) shown on each column for each outcome. Coefficients were estimated using OLS. T refers to the randomized individual-level treatment (equal to 1 if parents were sent text-messages and zero otherwise). Outcomes are behavior indicators built with answers to surveys (see Tables \ref{tab:scales_students_bl} and \ref{tab:scales_parents_bl} for details). For each scale, we take the item with the largest loading factor and build an indicator variable that takes value 1 when student/guardian answer 3 or 4 in the four scale. Items are (student versions): 'I organize well my time to do my school work', I am sure that I can understand the hardest things, My parents or guardians showed that they were proud of me, I went alone to school, My parents or guardians contacted teacher through e-mail, My parents or guardians congratulated me for my effort. Panel A shows results for scales built with answers parents gave to survey questions. Panel B shows results for scales built with answers students gave to survey questions. All models include the baseline math grade, attendance rate and outcome scales, classroom (randomization strata), and year fixed effects. If baseline values of baseline math grade/attendance or baseline outcomes were missing, we imputed them using the classroom-level mean and added an indicator variable for these imputed observations. Standard errors are clustered at the classroom level (shown in brackets).$^{\dagger}$ Number of observations vary by column because of survey and item non-response. * significant at 10\%; ** significant at 5\%; *** significant at 1\%." ;

#delimit cr

  
texsave using "$path/Auxiliary/results/Table_ScalesDummy.tex", width(1.2\textwidth) ///
replace size(scriptsize) align(lCCCCCC) location(ht) varlabels frag hlines(8) ///
title(Treatment Effects on Parental Behavior at Home: Indicators) marker(tab:parentalbehavior_dummy) ///
footnote("$note1", size(scriptsize)) nofix


